package cn.tedu.ivos.user.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.pojo.dto.UserLoginParam;
import cn.tedu.ivos.user.pojo.dto.UserQuery;
import cn.tedu.ivos.user.pojo.dto.UserSaveParam;
import cn.tedu.ivos.user.pojo.entity.User;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Closeable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Override
    public UserVO login(UserLoginParam userLoginParam) {
        log.debug("登录业务：参数={}",userLoginParam);
        //根据前端传来的用户名查询数据库中是否有此数据
        UserVO userVO = userMapper.selectByUsername(userLoginParam.getUsername());
        if (userVO == null){
            //查询到用户名不存在说明用户名错误，抛出业务层异常进入全局异常处理
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        if (!userVO.getPassword().equals(userLoginParam.getPassword())){
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        //如果没有抛出异常，业务层将正常处理完毕的userVO结果返回给Controller层，再由Controller层响应给前端
        return userVO;
    }

    @Override
    public void userSave(UserSaveParam userSaveParam) {
        log.debug("保存用户业务，参数={}",userSaveParam);
        User user = new User();
        BeanUtils.copyProperties(userSaveParam,user);
        //判断是新增还是修改操作，无id值说明是新增
        if (user.getId() == null){
            user.setPassword("123456");
            user.setCreateTime(new Date());
            userMapper.insert(user);
        }else {
            user.setUpdateTime(new Date());
            userMapper.update(user);
        }
    }

    @Override
    public List<UserVO> selectUser(UserQuery userQuery) {
        log.debug("查询用户业务，参数:{}",userQuery);
        List<UserVO> list = userMapper.selectUser(userQuery);
        return list;
    }

    @Override
    public void resetPassword(Long userId) {
        log.debug("userId = "+ userId);
        User user = new User();
        user.setId(userId);
        user.setPassword("root");
        user.setUpdateTime(new Date());
        userMapper.update(user);
    }

    @Override
    public void updateStatus(Long userId, String status) {
        log.debug("更新状态操作，参数：{},{}",userId,status);
        User user = new User();
        user.setId(userId);
        user.setStatus(status);
        user.setUpdateTime(new Date());
        userMapper.update(user);
    }

    @Override
    public void deleteUser(Long userId) {
        log.debug("删除用户业务，参数={}",userId);
        userMapper.deleteById(userId);
    }

    @Override
    public List<UserVO> selectAuditList(Long parentId) {
        log.debug("查询审批人业务，参数={}",parentId);
        List<UserVO> list = new ArrayList<>();
        UserVO auditUser1 = userMapper.selectById(parentId);
        list.add(auditUser1);
        //判断是否有直属领导，且直属领导是否还有上级
        if (auditUser1 != null && auditUser1.getParentId() != null){
            UserVO auditUser2 = userMapper.selectById(auditUser1.getParentId());
            list.add(auditUser2);
        }
        return list;
    }

}
